/* File:        	Client.java
* Package:			com.tecnalia.authz.pep
* Created:      	29-abr-2011
* Last Changed: 	0:11:09
* Author:       	Borja Urkizu borja.urquizu@tecnalia.com
*
*   
* History:
*  Revision 1.0.0.0  
* 
* Tecnalia Research and Innovation
*/
package com.tecnalia.authz.pep.comm;

import java.io.*;
import java.net.*;

import org.jboss.security.xacml.interfaces.RequestContext;
import org.jboss.security.xacml.interfaces.ResponseContext;

import com.tecnalia.authz.common.comm.ProtocolImpl;
import com.tecnalia.authz.common.log.PDPLog;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


// TODO: Auto-generated Javadoc
/**
 * The Class Client.
 */
public class Client implements Runnable {
   
	   /** The logger. */
	   public PDPLog logger = new PDPLog(this.getClass());	
	   
	   /** The PD phost. */
	   private String PDPhost = "localhost";
	   
	   /** The PD pport. */
	   private int PDPport = 50000;
	   
	   /**
	    * Instantiates a new client.
	    *
	    * @param host the host
	    * @param port the port
	    */
	   public Client(String host, int port){
		   PDPhost = host;
		   PDPport = port;
	   }
	
	   /**
	    * XACM lquery.
	    *
	    * @param request the request
	    * @param ID the iD
	    * @param name the name
	    * @param queryMode the query mode
	    * @return the response context[]
	    * @throws Exception the exception
	    */
	   public ResponseContext[] XACMLquery (RequestContext[] request,
			   								int ID,
			   								String name,
			   								int queryMode) throws Exception{
		    ObjectOutputStream oos = null;
		    ObjectInputStream ois = null;
		
			//Create the object to exchange between Server and client
			ProtocolImpl requestSerialize = new ProtocolImpl(ID, name, queryMode);
	
			// open a socket connection
			Socket socket = new Socket(PDPhost, PDPport);
			// open I/O streams for objects
		    oos = new ObjectOutputStream(socket.getOutputStream());
		    ois = new ObjectInputStream(socket.getInputStream());
		  
		    requestSerialize.serializeRequest(request);
		
		    // write the objects to the server
			oos.writeObject(requestSerialize);
			oos.flush();
			
			// read an object from the server
		    ProtocolImpl responseSerialize = (ProtocolImpl) ois.readObject();
		  
		    if ( 0 != responseSerialize.getError()){
		    	Exception e = new Exception("ERROR "+ responseSerialize.getError() + " " + responseSerialize.getErrorString());
		    	e.fillInStackTrace();
			
				// after unpacking the array, iterate through it
			    oos.close();
			    ois.close();
		    
			    Thread.yield();
				
			    throw e;
				    
			}
			
			ResponseContext[] response = responseSerialize.deserializeResponse();
			
			
			// after unpacking the array, iterate through it
			oos.close();
			ois.close();
			
			Thread.yield();
	        
	      
			return response;
	   }
		   
	   /* (non-Javadoc)
	    * @see java.lang.Runnable#run()
	    */
	   @Override
	   public void run() {
	   	
	   }

}

